﻿
#include "PoiSearchViewController.h"

PoiSearchViewController::PoiSearchViewController()
{

}

PoiSearchViewController::~PoiSearchViewController()
{

}

void PoiSearchViewController::viewDidLoad()
{
    // Do any additional setup after loading the view from its nib.

	this->setNavigationBarItem(CANavigationBarItem::create(UTF8("Poi搜索")));

	const DSize& viewSize = this->getView()->getFrame().size;

	AppTool::getInstance()->setPoiSearchDelegate(this);

	CAButton* poikeybt = CAButton::createWithFrame(DRect(50, 100, 200, 90), CAButtonTypeRoundedRect);
	poikeybt->setTitleFontSize(30);
	poikeybt->setTitleForState(CAControlStateAll, UTF8("Key搜索"));
	poikeybt->setTitleColorForState(CAControlStateAll, CAColor_black);
	poikeybt->addTarget(this, CAControl_selector(PoiSearchViewController::poikeyBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(poikeybt);

	lb1 = CALabel::createWithFrame(DRect(20, 100 + 100, viewSize.width - 20 * 2, 300));
	lb1->setFontSize(30);
	lb1->setColor(CAColor_black);
	lb1->setTextAlignment(CATextAlignmentLeft);
	lb1->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb1->setText(UTF8("Key搜索"));
	this->getView()->addSubview(lb1);

	CAButton* aroundbt = CAButton::createWithFrame(DRect(50, 100 + 100 + 300, 200, 90), CAButtonTypeRoundedRect);
	aroundbt->setTitleFontSize(30);
	aroundbt->setTitleForState(CAControlStateAll, UTF8("周边搜索"));
	aroundbt->setTitleColorForState(CAControlStateAll, CAColor_black);
	aroundbt->addTarget(this, CAControl_selector(PoiSearchViewController::poiaroundBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(aroundbt);

	lb2 = CALabel::createWithFrame(DRect(20, 100 + 100 + 300 + 100, viewSize.width - 20 * 2, 300));
	lb2->setFontSize(30);
	lb2->setColor(CAColor_black);
	lb2->setTextAlignment(CATextAlignmentLeft);
	lb2->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb2->setText(UTF8("周边搜索"));
	this->getView()->addSubview(lb2);
}

void PoiSearchViewController::viewDidUnload()
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

void PoiSearchViewController::poikeyBtClicked(CAControl* ctrl, DPoint point)
{
	AppTool::getInstance()->poiKeySearch(UTF8("万达影城"), UTF8("电影院"), UTF8("北京"));
}

void PoiSearchViewController::poiaroundBtClicked(CAControl* ctrl, DPoint point)
{
	AppTool::getInstance()->poiAroundSearch(UTF8("沃尔玛"), UTF8(""), 39.976587, 116.339925, 5000, UTF8("北京"));
}

void PoiSearchViewController::onPoiKeySearchEnd(int code, std::vector<POISearchData> v_datas)
{
	m_iCode = code;
	v_PoiDatas = v_datas;

	CAScheduler::schedule(schedule_selector(PoiSearchViewController::poiKeySearchSchedule), this, 0);
}

void PoiSearchViewController::poiKeySearchSchedule(float t)
{
	CAScheduler::unschedule(schedule_selector(PoiSearchViewController::poiKeySearchSchedule), this);

	lb1->setText(UTF8("Key搜索:") + crossapp_format_string("code:%d\nnum:%d", m_iCode, (int)v_PoiDatas.size()));
}

void PoiSearchViewController::onPoiAroundSearch(int code, std::vector<POISearchData> v_datas)
{
	m_iCode = code;
	v_PoiDatas = v_datas;

	CAScheduler::schedule(schedule_selector(PoiSearchViewController::poiAroundSearchSchedule), this, 0);
}

void PoiSearchViewController::poiAroundSearchSchedule(float t)
{
	CAScheduler::unschedule(schedule_selector(PoiSearchViewController::poiAroundSearchSchedule), this);

	lb2->setText(UTF8("周边搜索:") + crossapp_format_string("code:%d\nnum:%d", m_iCode, (int)v_PoiDatas.size()));
}


